<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<ui:composition 
	  xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      template="/view/templates/templateLayout.xhtml">
      
      <ui:define name="conteudo">      	
  	  <link rel="stylesheet" type="text/css" href="./estilos.css"/>
  	  
<h:form id="formManterCompra" prependId="false">  
<h:panelGroup id="pgManterCompra">
	<p:panel id="panelManterCompra" header="Manter Compra" style="margin-bottom:20px; ">  
	<f:facet name="header">
		<h:outputText value="Nova Compra" styleClass="outputTextTitulo"/>
	</f:facet>
 	<p:messages id="messagesManterCompra" autoUpdate="true" closable="true" />  
    	<h:panelGrid id="pgCompra" columns="6" style="margin-top:20px; margin-right:10%;" >  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Nº Nota*" styleClass="outputLabel"/>
            <h:outputLabel value="Nº Série*" styleClass="outputLabel"/>   
            <h:outputLabel value="Pedido" styleClass="outputLabel"/>   
            <h:outputLabel value="Carga" styleClass="outputLabel"/>    
            <h:outputLabel value="Chave de acesso" styleClass="outputLabel"/>       
            <p:inputText value="#{compraBean.compra.idCompra}" disabled="true" styleClass="inputId"/>     
            <p:inputText value="#{compraBean.compra.numeroNota}" styleClass="inputData" maxlength="20" disabled="#{compraBean.disableSave()}" required="true" requiredMessage="Informar o Número da Nota!"/>  
  			<p:inputText value="#{compraBean.compra.numeroSerie}"  styleClass="inputData" maxlength="20" disabled="#{compraBean.disableSave()}" required="true" requiredMessage="Informar o Número de Série!"/>
  			<p:inputText value="#{compraBean.compra.pedido}"  styleClass="inputData" maxlength="20" disabled="#{compraBean.disableSave()}"/>
  			<p:inputText value="#{compraBean.compra.carga}"  styleClass="inputData" maxlength="20" disabled="#{compraBean.disableSave()}"/>
  			<p:inputMask value="#{compraBean.compra.chaveAcesso}" mask="9999.9999.9999.9999.9999.9999.9999.9999.9999.9999.9999" disabled="#{compraBean.disableSave()}"
  						 styleClass="inputDescricao" maxlength="44" style="width:350px"/>
        </h:panelGrid> 
        <h:panelGroup>
        <h:panelGrid id="pgDate" columns="2" style="margin-right:10%;">
            <h:outputLabel value="Data de Emissão*" styleClass="outputLabel"/>
            <h:outputLabel value="Data de Entrega*" styleClass="outputLabel"/> 
  			<p:inputText value="#{compraBean.compra.dtEmissao}"  styleClass="inputData" disabled="#{compraBean.disableSave()}" required="true" requiredMessage="Informar a data de emissão!">
  				<f:convertDateTime dateStyle="medium"/>
  				<p:ajax  listener="#{compraBean.validaData}" update="pgDate" />
  			</p:inputText>
  			<p:inputMask mask="99/99/9999" value="#{compraBean.compra.dtEntrega}" styleClass="inputData" disabled="#{compraBean.disableSave()}" required="true" requiredMessage="Informar a data de entrega!">
  				<f:convertDateTime dateStyle="medium"/>
  				<p:ajax  listener="#{compraBean.validaData}" render="pgDate" />
  			</p:inputMask>  
        </h:panelGrid> 
        </h:panelGroup>
        <h:panelGroup>
        <h:panelGrid id="pgFornecedor" columns="4" style="margin-right:10%;">  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Fornecedor / Razão Social*" styleClass="outputLabel"/>     
            <h:outputLabel  />    
            <h:outputLabel value="CNPJ" styleClass="outputLabel"/>       
            <p:inputText value="#{compraBean.compra.fornecedor.idFornecedor}" maxlength="5" styleClass="inputId" disabled="#{compraBean.disableSave()}">
            	<p:ajax  listener="#{compraBean.onChangeFornecedor}"  />
            </p:inputText>  
  			<p:inputText  value="#{compraBean.compra.fornecedor.razaoSocial}" maxlength="40" styleClass="inputDescricao" disabled="#{compraBean.disableSave()}" >
  				<p:ajax listener="#{compraBean.onChangeFornecedorNull}" update="pgFornecedor"/>
  			</p:inputText>
  			<p:commandButton icon="ui-icon-search" actionListener="#{compraBean.openDialogConsultarFornecedor}" disabled="#{compraBean.disableSave()}" immediate="true"/>
  			<p:inputText value="#{compraBean.compra.fornecedor.cnpj}" styleClass="inputData" disabled="true"/>
        </h:panelGrid>    
        </h:panelGroup>
        <h:panelGroup>
        <h:panelGrid id="pgProduto" columns="4" style="margin-right:10%;" rendered="#{compraBean.renderedSave()}">  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Produto*" styleClass="outputLabel"/>  
            <h:outputLabel  value="Marca"/>
            <h:outputLabel  />
            <p:inputText  value="#{compraBean.itemCompra.produto.idProduto}" styleClass="inputId" >
            	<p:ajax  listener="#{compraBean.onChangeProduto}" update="pgProduto" />
            </p:inputText> 
  			<p:inputText  value="#{compraBean.itemCompra.produto.descricao}" maxlength="40" styleClass="inputDescricao" >
            	<p:ajax listener="#{compraBean.onChangeProdutoNull}" update="pgProduto" />
			</p:inputText>
			<p:inputText value="#{compraBean.itemCompra.produto.marca.descricao}" styleClass="inputDescricao" disabled="true"/>
  			<p:commandButton  icon="ui-icon-search" ajax="true" actionListener="#{compraBean.openDialogConsultarProduto}" immediate="true" />  			  
        </h:panelGrid> 
        </h:panelGroup>
        <h:panelGroup>
       	<h:panelGrid id="pgItemCompra" columns="8" style="margin-right:10%;" rendered="#{compraBean.renderedSave()}">  
            <h:outputLabel value="Quantidade" styleClass="outputLabel"/>  
            <h:outputLabel value="Preço(R$)*" styleClass="outputLabel"/> 
            <h:outputLabel  value="Alíquota ICMS(%)" styleClass="outputLabel"/> 
            <h:outputLabel  value="BC ICMS(R$)" styleClass="outputLabel"/>
            <h:outputLabel  value="Valor ICMS(R$)" styleClass="outputLabel"/>       
            <h:outputLabel  value="Alíquota IPI(%)" styleClass="outputLabel"/>     
            <h:outputLabel  value="Valor IPI(R$)" styleClass="outputLabel"/>
            <h:outputLabel  />
              			  
  			<p:inputText value="#{compraBean.itemCompra.quantidade}" style="width:80px;" styleClass="inputId" maxlength="5"  >
  				<f:convertNumber type="number"/>
  				<p:ajax listener="#{compraBean.calculaBCIcms}" update="inputBcIcms" />
  				<p:ajax listener="#{compraBean.ajaxGet}"/>
  			</p:inputText>
  			<p:inputText value="#{compraBean.itemCompra.precoCusto}" maxlength="7" style="width:100px;" styleClass="inputId">
  				<f:convertNumber type="number"/>
 				<p:ajax  listener="#{compraBean.calculaBCIcms}" update="inputBcIcms" />
  			</p:inputText>
  			<p:inputText value="#{compraBean.itemCompra.aliquotaIcms}" maxlength="5" style="width:120px;" styleClass="inputId">
  				<f:convertNumber type="number"/>
  				<p:ajax  listener="#{compraBean.calculaValorIcms}" update="inputValorIcms" />
  			</p:inputText>
  			<p:inputText id="inputBcIcms" value="#{compraBean.itemCompra.bcIcms}" maxlength="7" style="width:100px;" styleClass="inputId">
  				<f:convertNumber type="number"/>
  			</p:inputText>
  			<p:inputText id="inputValorIcms" value="#{compraBean.itemCompra.valorIcms}" maxlength="7" style="width:100px;" styleClass="inputId">
  				<f:convertNumber type="number"/>
  			</p:inputText>
			<p:inputText value="#{compraBean.itemCompra.aliquotaIpi}" maxlength="5" style="width:120px;" styleClass="inputId">
				<f:convertNumber type="number"/>
  			</p:inputText>
  			<p:inputText value="#{compraBean.itemCompra.valorIpi}" maxlength="7" style="width:100px;" styleClass="inputId">
  				<f:convertNumber type="number"/>
  			</p:inputText>
  			<p:commandButton icon="ui-icon-circle-plus" action="#{compraBean.addItemCompra}" update="messagesManterCompra, pgItemCompra, pgCompra, pgDate, pgProduto, tableItemCompra, btCompraCondPgto" />
        </h:panelGrid>         
        </h:panelGroup>
        <p:dataTable  id="tableItemCompra" value="#{compraBean.dmItemCompra}" var="itemCompra" style="margin-top:30px; width:90%"  
        				rowKey="#{itemCompra.idItemCompra}" selection="#{compraBean.itemCompraSelect}" >
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Código" />
				</f:facet>
				<h:outputText value="#{itemCompra.produto.idProduto}" />
			</p:column>
			<p:column style="width:20%">
				<f:facet name="header">
					<h:outputText value="Produto" />
				</f:facet>
				<h:outputText value="#{itemCompra.produto.descricao}" />
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Quantidade" />
				</f:facet>
				<h:outputText value="#{itemCompra.quantidade}" />
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Preço Unitário" />
				</f:facet>
				<h:outputText value="#{itemCompra.precoCusto}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2" minFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Preço Total" />
				</f:facet>
				<h:outputText value="#{itemCompra.precoTotal}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2" minFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="BC ICMS" />
				</f:facet>
				<h:outputText value="#{itemCompra.bcIcms}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2" minFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Valor ICMS" />
				</f:facet>
				<h:outputText value="#{itemCompra.valorIcms}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2" minFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Valor IPI" />
				</f:facet>
				<h:outputText value="#{itemCompra.valorIpi}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2" minFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Alíquota ICMS (%)" />
				</f:facet>
				<h:outputText value="#{itemCompra.aliquotaIcms}" >
					<f:convertNumber  />
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Alíquota IPI (%)" />
				</f:facet>
				<h:outputText value="#{itemCompra.aliquotaIpi}" >
					<f:convertNumber  />
				</h:outputText>
			</p:column>
       	 	<p:column style="width:4%; text-align:center" rendered="#{compraBean.renderedSave()}">  
        		<f:facet name="header">
        	 		<h:outputText value="Remover" />
        		</f:facet>
         		<p:commandButton icon="ui-icon-circle-minus" actionListener="#{compraBean.removeItem}" process="@this" update="tableItemCompra" />       			
       	 	</p:column >  
       	 	<f:facet name="footer" >
       	 		<h:outputText value="Total: R$ #{compraBean.somaValorTotal}" >
       	 			<f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
       	 		</h:outputText>
       	 	</f:facet>        
        </p:dataTable>
        <h:panelGroup>
        <h:panelGrid id="pgTributos" columns="5" style="margin-right:10%;">  
           	<h:outputLabel value="BC ICMS" styleClass="outputLabel"/>
           	<h:outputLabel value="Valor ICMS " styleClass="outputLabel"/>
           	<h:outputLabel value="Valor IPI" styleClass="outputLabel"/> 
           	<h:outputLabel value="BC ICMS Subst." styleClass="outputLabel"/>
           	<h:outputLabel value="Valor ICMS Subst." styleClass="outputLabel"/>          	          	
           	<p:inputText value="#{compraBean.compra.bcIcms}" style="width:70px" disabled="true" /> 
 			<p:inputText value="#{compraBean.compra.valorIcms}" style="width:70px" disabled="true"/>
 			<p:inputText value="#{compraBean.compra.valorIpi}" style="width:70px" disabled="true"/>
 			<p:inputText value="#{compraBean.compra.bcIcmsSubst}" maxlength="7" style="width:90px" disabled="#{compraBean.disableSave()}">
 				<f:convertNumber type="number"/>
  			</p:inputText>
 			<p:inputText value="#{compraBean.compra.valorIcmsSubst}" maxlength="7" style="width:105px" disabled="#{compraBean.disableSave()}" >
 				<f:convertNumber type="number"/>
  			</p:inputText>			
        </h:panelGrid> 
        </h:panelGroup>
        <h:panelGroup>
        <h:panelGrid id="pgTotal" columns="5" style="margin-right:10%;">  
           	<h:outputLabel value="Frete (R$)" styleClass="outputLabel"/>
           	<h:outputLabel value="Seguro (R$) " styleClass="outputLabel"/>
           	<h:outputLabel value="Outras Despesas Acessórias (R$)" styleClass="outputLabel"/>
           	<h:outputLabel value="Desconto (%)" styleClass="outputLabel"/>
           	<h:outputLabel value="Total da Compra(R$)" styleClass="outputLabel"/>            	
           	<p:inputText value="#{compraBean.compra.frete}" maxlength="6" styleClass="inputId" style="width:70px" disabled="#{compraBean.disableSave()}">
           		<f:convertNumber type="number"/>
           		<p:ajax  listener="#{compraBean.atualizaTotal}" />
           	</p:inputText>
           	<p:inputText value="#{compraBean.compra.seguro}" maxlength="6" styleClass="inputId" style="width:80px" disabled="#{compraBean.disableSave()}">
           		<f:convertNumber type="number"/>
           		<p:ajax  listener="#{compraBean.atualizaTotal}"  />
           	</p:inputText> 
           	<p:inputText value="#{compraBean.compra.despesasAcessorias}" styleClass="inputId" maxlength="6" style="width:210px" disabled="#{compraBean.disableSave()}">
           		<f:convertNumber type="number"/>
           		<p:ajax  listener="#{compraBean.atualizaTotal}"  />
           	</p:inputText> 
           	<p:inputText value="#{compraBean.compra.desconto}" maxlength="5" styleClass="inputId" style="width:90px" disabled="#{compraBean.disableSave()}">
           		<f:convertNumber type="number"/>
           		<p:ajax  listener="#{compraBean.atualizaTotal}" update="pgCondicaoPagamento" />
           	</p:inputText>            	
  			<p:inputText id="idValorTotal" value="#{compraBean.compra.valorTotal}" disabled="true"  >
  				<f:convertNumber type="number"/>
  			</p:inputText>
        </h:panelGrid> 
		</h:panelGroup>
        	<h:panelGrid id="pgCondicaoPagamento" columns="5" style="margin-right:10%;">  
           	 	<h:outputLabel value="Código" styleClass="outputLabel"/>  
            	<h:outputLabel value="Condição de Pagamento*" styleClass="outputLabel"/>      
            	<h:outputLabel  />       
            	<h:outputLabel value="Código" styleClass="outputLabel"/>  
            	<h:outputLabel value="Forma de Pagamento*" styleClass="outputLabel"/>    
            	<p:inputText value="#{compraBean.compra.condicaoPagamento.idCondicaoPagamento}" styleClass="inputId" disabled="true"/>  
  				<p:inputText  value="#{compraBean.compra.condicaoPagamento.descricao}"  styleClass="inputDescricao" style="width:150px;" immediate="true" disabled="true"/>  
  				<p:commandButton id="btCompraCondPgto" icon="ui-icon-search" actionListener="#{compraBean.openDialogConsultarCondicaoPagamento}" immediate="true" disabled="#{compraBean.disableCondicao()}" />  
            	<p:inputText value="#{compraBean.compra.condicaoPagamento.formaPagamento.idFormaPagamento}" styleClass="inputId" disabled="true"/>  
  				<p:inputText value="#{compraBean.compra.condicaoPagamento.formaPagamento.descricao}"  styleClass="inputDescricao" style="width:150px;" disabled="true"/>  
        	</h:panelGrid> 
        	<p:dataTable  id="tableParcela" value="#{compraBean.listContaPagar}" var="cr" style="margin-top:30px; width:30%">        				
				<p:column style="width:30%; text-align:center">
					<f:facet name="header">
						<h:outputText value="Data de Vencimento" />
					</f:facet>
					<h:outputText value="#{cr.dtVencimento}" >
						<f:convertDateTime dateStyle="medium"/>
					</h:outputText>
				</p:column>
				<p:column style="text-align:center">
					<f:facet name="header">
						<h:outputText value="Valor" />
					</f:facet>
					<h:outputText value="#{cr.valor}" >
						<f:convertNumber pattern="R$ ###,###.##" maxFractionDigits="2" minFractionDigits="2"/>
					</h:outputText>
				</p:column> 
				<p:column style="text-align:center">
					<f:facet name="header">
						<h:outputText value="Status" />
					</f:facet>
					<h:outputText value="#{cr.status}" />
				</p:column>    
    	    </p:dataTable>
    </p:panel>  
    <p:commandButton value="Salvar" action="#{compraBean.saveCompra}" update="pgManterCompra" styleClass="commandButton" rendered="#{compraBean.renderedSave()}"/> 
	<p:commandButton value="Voltar" action="#{compraBean.consultarCompra}" immediate="true" styleClass="commandButton" /> 
</h:panelGroup>
</h:form>  	
<ui:include src="/view/compra/dialogConsultarFornecedor.xhtml"></ui:include>
<ui:include src="/view/compra/dialogConsultarProduto.xhtml"></ui:include>
<ui:include src="/view/compra/dialogConsultarCondicaoPagamento.xhtml"></ui:include>
</ui:define>
</ui:composition>
